package com.example;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;

public class ListViewExample2 extends Activity {
    private String CLASSNAME = this.getClass().getSimpleName();
    private ArrayAdapter<String> mAdapter;
    private ArrayList<String> mData;
    private ListView mList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String[] data = {"중구","서구","동구"
                         ,"영도구","부산진구","동래구"
                         ,"남구","북구","해운대구"
                         ,"사하구","금정구","강서구"
                         ,"연제구","수영구","사상구"
                         ,"중구","기장군"};
        mData = new ArrayList<String>(Arrays.asList(data));
        
        MyAdapter adapter = new MyAdapter(this);
        mList = (ListView)findViewById(R.id.list);
        mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        mList.setAdapter(adapter);

        Button btnInput = (Button)findViewById(R.id.ok);
        btnInput.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuilder sb = new StringBuilder();
                sb.append("다음이 선택되었어요.\n");
                for (long id: mList.getCheckItemIds()) {
                    sb.append("" + id + ":" + mData.get((int)id));
                    sb.append("\n");
                }
                String text = new String(sb);
                Toast.makeText(ListViewExample2.this, text, Toast.LENGTH_LONG).show();
            }
        });
    }
    
    class MyAdapter extends BaseAdapter{
        
        private int background_normal = Color.parseColor("#ff000000");
        private int background_selected = Color.parseColor("#44888888");
        private LayoutInflater mInflater;
        
        public MyAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //화면에 보여지기 직전에 불려지며, 화면에 불려지는 아이템보다 위로 하나, 아래로 하나 더의 생성된다. 
            ViewHolder holder;
            if (convertView == null) {
//                Log.e(CLASSNAME, "[getView] convertView == null");
                convertView = mInflater.inflate(R.layout.item, null);
                holder = new ViewHolder();
                holder.view = (TextView)convertView.findViewById(R.id.name);
                holder.icon = (TextView)convertView.findViewById(R.id.check);
                convertView.setTag(holder);
            } else {
//                Log.e(CLASSNAME, "[getView] convertView != null");
                holder = (ViewHolder)convertView.getTag();
            }
            
            String item = mData.get(position);
            holder.view.setText("" + position + ":" +item);
            
            Log.e(CLASSNAME, "[getView] " + "" + position + ":" +item);
            
//            String s = (holder.icon == null?"Null":"Not Null");
//            Log.e(CLASSNAME, "[getView] icon:" + s);
            
            //isItemChecked() 리스트의 position자리에 아이템이 존재하지 않을시 false를 리턴한다.
            boolean checked = mList.isItemChecked(position);
            boolean buttonless = true;
            if (buttonless) {
                holder.icon.setBackgroundResource(R.drawable.btn_check_buttonless_on);
                holder.icon.setVisibility(checked ? View.VISIBLE : View.GONE);
            } else {
                holder.icon.setBackgroundResource(
                        checked ? R.drawable.btn_check_on : R.drawable.btn_check_off);
            }

            convertView.setBackgroundColor(checked ? background_selected : background_normal);
            return convertView;
        }
    }
    
    class ViewHolder {
        public TextView view;
        public TextView icon;
    }
}